class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == nullptr || head->next == nullptr) return head;
        ListNode* newHead = new ListNode(0);
        newHead->next = head;
        ListNode* prev = newHead, * cur = head, * next = head->next, * nnext = next->next;
        while (cur != nullptr && next != nullptr)
        {
            next->next = cur;
            cur->next = nnext;
            prev->next = next;

            prev = cur;
            cur = nnext;
            if (cur) next = cur->next;
            if (next) nnext = next->next;
        }
        return newHead->next;
    }
};